const TOKEN_KEY = 'geek_mobile_22'
const USER_CHANNELS = 'geek_channels'

/**
 * 从本地缓存中获取 Token 信息
 */
export const getTokenInfo = () => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY)) || {}
}

/**
 * 将 Token 信息存入缓存
 * @param {Object} tokenInfo 从后端获取到的 Token 信息
 */
export const setTokenInfo = (tokenInfo) => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(tokenInfo))
}

/**
 * 删除本地缓存中的 Token 信息
 */
export const removeTokenInfo = () => {
  localStorage.removeItem(TOKEN_KEY)
}

/**
 * 判断本地缓存中是否存在 Token 信息
 */
export const hasToken = () => {
  return !!getTokenInfo().token
}

/**
 * 将channels保存到本地
 * @param {*} channels
 * @returns
 */
export const setLocaChannels = (channels) =>
  localStorage.setItem(USER_CHANNELS, JSON.stringify(channels))

/**
 * 获取本地channels
 * @returns
 */
export const getLocaChannels = () =>
  JSON.parse(localStorage.getItem(USER_CHANNELS))

/**
 * 删除本地channels
 * @returns
 */
export const removeLocaChannels = () => localStorage.removeItem(USER_CHANNELS)
